package cn.pangza.controller;

import cn.pangza.common.exception.BusinessException;
import cn.pangza.common.utils.ResponseData;
import cn.pangza.common.utils.ValidateUtil;
import cn.pangza.hr.employee.service.EmployeeService;
import cn.pangza.user.entity.dto.SystemUserDto;
import cn.pangza.user.entity.request.LoginRequest;
import cn.pangza.user.entity.vo.SystemUserVo;
import cn.pangza.user.service.SystemUserService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LoginController {
    @Autowired
    SystemUserService systemUserService;

    @Autowired
    EmployeeService employeeService;

    @PostMapping("/api/login")
    public ResponseData<SystemUserVo> login(@Validated @RequestBody LoginRequest loginRequest, BindingResult bindingResult) throws BusinessException {
        ResponseData responseData = ValidateUtil.doValidate(bindingResult);
        if(ObjectUtils.isNotEmpty(responseData)) return responseData;
        SystemUserDto systemUserDto = new SystemUserDto();
        systemUserDto.setUsername(loginRequest.getUsername());
        systemUserDto.setPassword(loginRequest.getPassword());
        try {
            SystemUserVo systemUserVo = systemUserService.login(systemUserDto);
            return ResponseData.success(systemUserVo);
        } catch (BusinessException e) {
            return ResponseData.error(e.getMessage());
        }
    }

    @GetMapping("/api/employee/pub/hello")
    public ResponseData<String> testEmployeeHello() {
        return ResponseData.success(employeeService.helloEmployee());
    }

}
